/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */
package org.hibernate.jpa.boot.internal;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import jakarta.persistence.Entity;
import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor;
import org.hibernate.boot.archive.scan.spi.ScanEnvironment;

/**
 * Implementation of ScanEnvironment leveraging a JPA deployment descriptor.
 *
 * @author Steve Ebersole
 */
public class StandardJpaScanEnvironmentImpl implements ScanEnvironment {
    private final PersistenceUnitDescriptor persistenceUnitDescriptor;

    private final List<String> explicitlyListedClassNames;
    private final List<String> explicitlyListedMappingFiles;


    public StandardJpaScanEnvironmentImpl(PersistenceUnitDescriptor persistenceUnitDescriptor) {
        this.persistenceUnitDescriptor = persistenceUnitDescriptor;

        // 杨森添加：自动扫描所有@Entity注解的类-----------开始
        List<String> 临时explicitlyListedClassNames = null;
        Class<?> 类工具Class = null;
        try
        {
            类工具Class = Class.forName("中国.易.工具.类工具");
        }
        catch (ClassNotFoundException e1)
        {
            // 没找到类也正常
        }

        if (类工具Class == null)
        {
            System.out.println("没找到类工具,则走原有流程");
            临时explicitlyListedClassNames = persistenceUnitDescriptor.getManagedClassNames() == null
                    ? Collections.<String>emptyList()
                    : persistenceUnitDescriptor.getManagedClassNames();
        }
        else
        {
            // 用这个值存包名
            List<String> 包名list = persistenceUnitDescriptor.getManagedClassNames();
            if (包名list == null)
            {
                临时explicitlyListedClassNames = Collections.<String>emptyList();
            }
            else
            {
                Method m;
                try
                {
                    m = 类工具Class.getMethod("获取类列表", String.class, Class.class);
                    临时explicitlyListedClassNames = new ArrayList<>();
                    for (String 包名 : 包名list)
                    {
                        @SuppressWarnings("unchecked")
                        Collection<? extends String> cs = (Collection<? extends String>) m.invoke(null, 包名,
                                Entity.class);
                        临时explicitlyListedClassNames.addAll(cs);
                    }
                }
                catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException |
                        InvocationTargetException e)
                {
                    e.printStackTrace();
                    临时explicitlyListedClassNames = persistenceUnitDescriptor.getManagedClassNames() == null ? Collections.<String>emptyList()
                            : persistenceUnitDescriptor.getManagedClassNames();
                }
            }
        }
        this.explicitlyListedClassNames = 临时explicitlyListedClassNames;

        //        this.explicitlyListedClassNames = persistenceUnitDescriptor.getManagedClassNames() == null
        //                ? Collections.emptyList()
        //                : persistenceUnitDescriptor.getManagedClassNames();
        // 杨森添加：自动扫描所有@Entity注解的类-----------结束
        this.explicitlyListedMappingFiles = persistenceUnitDescriptor.getMappingFileNames() == null
                ? Collections.emptyList()
                : persistenceUnitDescriptor.getMappingFileNames();
    }

    @Override
    public URL getRootUrl() {
        return persistenceUnitDescriptor.getPersistenceUnitRootUrl();
    }

    @Override
    public List<URL> getNonRootUrls() {
        return persistenceUnitDescriptor.getJarFileUrls();
    }

    @Override
    public List<String> getExplicitlyListedClassNames() {
        return explicitlyListedClassNames;
    }

    @Override
    public List<String> getExplicitlyListedMappingFiles() {
        return explicitlyListedMappingFiles;
    }
}
